package com.example.home.pet.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.pet.activity.CaseInquiryDetail;
import com.example.home.pet.bean.CaseInquiryBean;
import com.google.gson.Gson;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class CaseInquiryAdapter extends RecyclerView.Adapter<CaseInquiryAdapter.MyHoldy> {
    private Context context;
    private List<CaseInquiryBean.RowsBean> list;

    public CaseInquiryAdapter(Context context, List<CaseInquiryBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public CaseInquiryAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充视图
        View view = LayoutInflater.from(context).inflate(R.layout.item_case, null);
        return new CaseInquiryAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CaseInquiryAdapter.MyHoldy holder, int position) {
        CaseInquiryBean.RowsBean.DoctorBean doctor = list.get(position).getDoctor();
        if (doctor != null){
            Glide.with(context).load(ip+doctor.getAvatar()).into(holder.avatar);
            holder.name.setText(doctor.getName());
            String goodAt = doctor.getGoodAt();
            if (goodAt.length() >= 35){
                holder.goodAt.setText(goodAt.substring(0,35)+"...");
            }else {
                holder.goodAt.setText(goodAt);
            }
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CaseInquiryDetail.class);
                    Gson gson = new Gson();
                    intent.putExtra("CaseInquiryBean",gson.toJson(list.get(position)));
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView avatar;
        private TextView name;
        private TextView goodAt;
        private RelativeLayout rootView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.avatar);
            name = itemView.findViewById(R.id.name);
            goodAt = itemView.findViewById(R.id.goodAt);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
